package com.scenery;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.location.Location;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.LocationListener;
import com.scenery.services.SetManager;
import com.scenery.services.UserServices;
import com.scenery.util.HttpUtil;
//登陆
public class LoginActivty extends Activity {
	/**
	 * @see android.app.Activity#onCreate(Bundle)
	 */
	String tag=LoginActivty.class.getName();
	EditText etAccount,etPassword,serverIp,openFirePort,webserverPort;
	Button btnLogin,regitLogin;
	UserServices services;
	SetManager setmanager;
	ProgressDialog dialog;
	LocationListener mLocationListener = null;//create时注册此listener，Destroy时需要Remove
	String lon="",lat="";
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		// TODO Put your code here
		setContentView(R.layout.login);
		etAccount=(EditText) findViewById(R.id.etAccount);
		etPassword=(EditText) findViewById(R.id.etPassword);
		btnLogin=(Button)findViewById(R.id.btnLogin);
		regitLogin=(Button)findViewById(R.id.regitLogin);
		serverIp=(EditText) findViewById(R.id.serverIp);
		openFirePort=(EditText) findViewById(R.id.openFirePort);
		webserverPort=(EditText) findViewById(R.id.webserverPort);
		
		
		services=new UserServices();
		setmanager=new SetManager(LoginActivty.this);
		serverIp.setText(setmanager.getIp());
		etAccount.setText(setmanager.getUserId());
		openFirePort.setText(setmanager.getPort());
		webserverPort.setText(setmanager.getWebServerport());
		
		dialog=new ProgressDialog(LoginActivty.this);
		btnLogin.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(!HttpUtil.isNetWorkConnected(LoginActivty.this))
				{
					Toast.makeText(LoginActivty.this, "网络不通！", Toast.LENGTH_LONG).show();
				}else {
					
					String userid=etAccount.getText().toString();
					String password=etPassword.getText().toString();
					String ip=serverIp.getText().toString();
					String ofport=openFirePort.getText().toString();
					String webport=webserverPort.getText().toString();
					
					LoginAsyncTask login=new LoginAsyncTask();
					login.init(userid,password,ip);
					dialog.setMessage("登陆中");
					dialog.show();
					login.execute(0);
					
					setmanager.save(ip, ofport, userid,webport);
//					services.setIp(serverIp.getText().toString());
//					services.loginOpenFire(etAccount.getText().toString(),  etPassword.getText().toString());
				}
			}
		});
		regitLogin.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setClass(LoginActivty.this, Register.class);
				startActivity(intent);
			}
		});
		BMapApiDemoApp app = (BMapApiDemoApp)this.getApplication();
		if (app.mBMapMan == null) {
			app.mBMapMan = new BMapManager(getApplication());
			app.mBMapMan.init(app.mStrKey, new BMapApiDemoApp.MyGeneralListener());
		}
		app.mBMapMan.start();
		

        // 注册定位事件
        mLocationListener = new LocationListener(){

			@Override
			public void onLocationChanged(Location location) {
				if(location != null){
					String strLog = String.format("您当前的位置:\r\n" +
							"纬度:%f\r\n" +
							"经度:%f",
							location.getLongitude(), location.getLatitude());
					lon=location.getLongitude()+"";
					lat=location.getLatitude()+"";
					
			      Log.d(tag, strLog);
				}
			}
        };
	}
	class LoginAsyncTask extends AsyncTask<Integer,Integer,String>{
		
		String user="",pwd="",ip="";
		void init(String user,String pwd,String ip)
		{
			this.user=user;
			this.pwd=pwd;
			this.ip=ip;
		}
		
		@Override
		protected String doInBackground(Integer... params) {
			
			
			 //return services.login(user, pwd);
			services.setIp(ip);
			String ret=services.loginOpenFire(user, pwd);
			Log.d(tag, "doInBackground:"+ret);
			if(Integer.parseInt(ret.trim())==1)
			{
				services.postLoation(user, lon, lat);
			}
			return ret;
			
		}
		@Override
		protected void onPostExecute(String result) 
		{
			dialog.dismiss();
			Log.d(user, "onPostExecute:"+result);
			
			int ret=new Integer(result.trim());

			switch(ret)
			{
				case 1:
					Toast.makeText(LoginActivty.this, "成功", Toast.LENGTH_LONG).show();
					Intent intent=new Intent();
					intent.setClass(LoginActivty.this, MainActivity.class);
					startActivity(intent);
					break;
				case 2:
					Toast.makeText(LoginActivty.this, "用户名不存在", Toast.LENGTH_LONG).show();
					break;
				case 3:
					Toast.makeText(LoginActivty.this, "密码错误", Toast.LENGTH_LONG).show();
					break;
				case 4:
					Toast.makeText(LoginActivty.this, "程序异常", Toast.LENGTH_LONG).show();
					break;
			}
		}
		
	};
	@Override
	protected void onPause() {
		BMapApiDemoApp app = (BMapApiDemoApp)this.getApplication();
		// 移除listener
		app.mBMapMan.getLocationManager().removeUpdates(mLocationListener);
		app.mBMapMan.stop();
		super.onPause();
	}
	@Override
	protected void onResume() {
		BMapApiDemoApp app = (BMapApiDemoApp)this.getApplication();
		// 注册Listener
        app.mBMapMan.getLocationManager().requestLocationUpdates(mLocationListener);
		app.mBMapMan.start();
		super.onResume();
	}
}
